home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 / MACD 5.bin / workbench / libs / unixlib.lha / unix / test / socks / strchkread.c < prev    next >
C/C++ Source or Header  |  1996-10-22  |  3KB  |  116 lines

  1. #include <proto/socket.h>
  2. #include <sys/types.h>
  3. #include <sys/socket.h>
  4. #include <sys/time.h>
  5. #include <netinet/in.h>
  6. #include <netdb.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <unistd.h>
  11. #include <math.h>
  12. #include <fcntl.h>
  13. #include <sys/wait.h>
  14. #include <errno.h>
  15. #define TRUE 1
  16.  
  17. /*
  18.  * This program uses select() to check that someone is trying to connect
  19.  * before calling accept(). 
  20.  */
  21.  
  22. int
  23. main(void)
  24. {
  25.     int sock;
  26.     long length;
  27.     struct sockaddr_in server;
  28.     int msgsock;
  29.     char buf[1024];
  30.     int rval;
  31.     fd_set ready;
  32.     struct timeval to;
  33.     int pid;
  34.     int fifo_ok = 1;
  35.     int std_in[2];
  36.     int std_out[2];
  37.     char *argv[2];
  38.  
  39.     if (pipe(std_in) != 0 || pipe(std_out) != 0) {
  40.         printf("Cannot get a pipe.\n");
  41.         exit(1);
  42.     }
  43.     fcntl(std_out[0], F_SETFL, O_NONBLOCK);
  44.     argv[0] = "getaline";
  45.     argv[1] = NULL;
  46.     pid = exec(argv[0], argv, std_in[0], std_out[1], NULL, 4096);
  47.     if (pid < 0) {
  48.         printf("Cannot exec %s.\n", argv[0]);
  49.         exit(1);
  50.     }
  51.  
  52.     /* Create socket */
  53.     sock = socket(AF_INET, SOCK_STREAM, 0);
  54.     if (sock < 0) {
  55.         perror("opening stream socket");
  56.         exit(1);
  57.     }
  58.     /* Name socket using wildcards */
  59.     server.sin_family = AF_INET;
  60.     server.sin_addr.s_addr = INADDR_ANY;
  61.     server.sin_port = 0;
  62.     if (bind(sock, (struct sockaddr *)&server, sizeof(server))) {
  63.         perror("binding stream socket");
  64.         exit(1);
  65.     }
  66.     /* Find out assigned port number and print it out */
  67.     length = sizeof(server);
  68.     if (getsockname(sock, (struct sockaddr *)&server, &length)) {
  69.         perror("getting socket name");
  70.         exit(1);
  71.     }
  72.     printf("Socket has port #%d\n", ntohs(server.sin_port));
  73.  
  74.     /* Start accepting connections */
  75.     listen(sock, 5);
  76.     do {
  77.         FD_ZERO(&ready);
  78.         FD_SET(sock, &ready);
  79.         if (fifo_ok)
  80.             FD_SET(std_out[0], &ready);
  81.         to.tv_sec = 5;
  82.         to.tv_usec = 0;
  83.         if (select(max(sock,std_out[0]) + 1, &ready, 0, 0, &to) < 0) {
  84.             perror("select");
  85.             continue;
  86.         }
  87.         if (FD_ISSET(sock, &ready)) {
  88.             msgsock = accept(sock, (struct sockaddr *)0, (long *)0);
  89.             if (msgsock == -1)
  90.                 perror("accept");
  91.             else do {
  92.                 bzero(buf, sizeof(buf));
  93.                 if ((rval = read(msgsock, buf, 1024)) < 0)
  94.                     perror("reading stream message");
  95.                 else if (rval == 0)
  96.                     printf("Ending connection\n");
  97.                 else
  98.                     printf("-->%s\n", buf);
  99.             } while (rval > 0);
  100.             close(msgsock);
  101.         } else if (fifo_ok && FD_ISSET(std_out[0], &ready)) {
  102.             bzero(buf, sizeof(buf));
  103.             if ((rval = read(std_out[0], buf, 1024)) > 1)
  104.                 printf("-->%s", buf);
  105.             else if (errno != EAGAIN || rval == 1) {
  106.                 printf("Child exited\n");
  107.                 wait(0);
  108.                 close(std_out[0]);
  109.                 close(std_in[1]);
  110.                 fifo_ok = 0;
  111.             }
  112.         } else
  113.             printf("Do something else\n");
  114.     } while (TRUE);
  115. }
  116.